<?php

namespace backend\controllers;

use Yii;
use common\models\IcoOrder;
use common\search\IcoOrderSearch;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * IcoOrderController implements the CRUD actions for IcoOrder model.
 */
class IcoOrderController extends BaseController
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all IcoOrder models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new IcoOrderSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Lists all IcoOrder models.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionList()
    {
        $this->returnJson = true;
        $searchModel = new IcoOrderSearch();
        $searchModel->uid = $this->member->id;
        $data['IcoOrderSearch'] = Yii::$app->request->queryParams;
        $dataProvider = $searchModel->search($data);
        $list = $dataProvider->getModels();
        return $this->renderPageList($dataProvider,$list);
    }

    /**
     * Lists all IcoOrder models.
     * <roles>
     * @loginrequired
     * </roles>
     * @return mixed
     */
    public function actionDetail()
    {
        $this->returnJson = true;
        $id = Yii::$app->request->post('id',0);
        $model = $this->findModel($id);
        if($model->uid!=$this->member->id){
            throw new NotFoundHttpException('订单不存在！');
        }
        return $this->renderJson(0,'ok',$model);
    }

    /**
     * Displays a single IcoOrder model.
     * @param string $id
     * @return mixed
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * 下单
     * Creates a new IcoOrder model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>
     * @loginrequired
     * @google_verify
     * </roles>
     * @return mixed
     */
    public function actionCreate()
    {
        $this->returnJson = true;
        $model = new IcoOrder();
        $model->load(Yii::$app->request->post(),'');
        $model->status = IcoOrder::STATUS_PAY_ING;
        $model->uid = $this->member->id;
        $model->save();
        return $this->renderJson();
    }

    /**
     * 增加收币地址
     * Creates a new IcoOrder model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * <roles>@loginrequired</roles>
     * @return mixed
     */
    public function actionAddAddress()
    {
        $this->returnJson = true;
        $orderId = Yii::$app->request->post('id',0);
        $model = $this->findModel($orderId);
        if($model->status!=IcoOrder::STATUS_NEED_ADDRESS){
            throw new NotFoundHttpException('状态不对!');
        }
        $model->status = IcoOrder::STATUS_NEED_SEND;
        $model->recieve_address = Yii::$app->request->post('recieve_address',0);
        if(empty($model->recieve_address)){
            throw new NotFoundHttpException('收币地址不能为空!');
        }
        $model->save();
        return $this->renderJson();
    }

    /**
     * Updates an existing IcoOrder model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param string $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Deletes an existing IcoOrder model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param string $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the IcoOrder model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param string $id
     * @return IcoOrder the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = IcoOrder::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
